/**
 * 格式化年月日
 */
const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(_formatNumber).join('/') + ' ' + [hour, minute, second].map(_formatNumber).join(':')
}

const _formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

const computeDate = (startDate = new Date(), interval = 7) => {
    let startTime = new Date(Date.parse(startDate)).getTime();
    let gapTime = interval * 60 * 60 * 24 * 1000;
    let endTime = new Date(Math.abs(startTime + gapTime))
    return formatTime(endTime)
}

module.exports = {
    formatTime: formatTime,
    computeDate: computeDate
}